package com.niit.house.tgq.price;


import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;




public class PriceReducer extends Reducer<Text, PriceWritable, Text, Text> {
    int[] SumPrice = {0, 0, 0, 0, 0, 0, 0};

    int[] houses = {1, 1, 1, 1, 1, 1, 1};





    @Override
    protected void reduce(Text key, Iterable<PriceWritable> values, Context context) throws IOException, InterruptedException {

        for (PriceWritable val : values) {
            if ((val.getOwnership()).equals("集资房")) {
                SumPrice[0] += val.getPrice();
                houses[0]++;
            } else if ((val.getOwnership()).equals("已购公房")) {
                SumPrice[1] += val.getPrice();
                houses[1]++;
            } else if ((val.getOwnership()).equals("限价商品房")) {
                SumPrice[2] += val.getPrice();
                houses[2]++;
            } else if ((val.getOwnership()).equals("拆迁安置房")) {
                SumPrice[3] += val.getPrice();
                houses[3]++;
            } else if ((val.getOwnership()).equals("商品房")) {
                SumPrice[4] += val.getPrice();
                houses[4]++;
            } else if ((val.getOwnership()).equals("经济适用房")) {
                SumPrice[5] += val.getPrice();
                houses[5]++;
            }
            SumPrice[6] += val.getPrice();
            houses[6]++;
        }

        context.write(new Text(String.format("%s", key.toString())), new Text(String.format("平均房价: %d, 集资房: %d, 已购公房: %d, 限价商品房: %d, 拆迁安置房: %d, 商品房: %d, 经济适用房: %d", SumPrice[6] / houses[6], SumPrice[0]/houses[0], SumPrice[1]/houses[1], SumPrice[2]/houses[2], SumPrice[3]/houses[3], SumPrice[4]/houses[4], SumPrice[5]/houses[5])));

    }




}
